import cv2
import numpy as np
img = cv2.imread(r"/\images\005.jpeg")

height = img.shape[0]
width = img.shape[1]
dsize = (width, height)
img1 = cv2.resize(img, None, fx=0.5, fy=0.5)

img2 = cv2.flip(img, 0)
img3 = cv2.flip(img, 1)
img4 = cv2.flip(img, 2)

# 转换矩阵 [[1, 0, m], [0, 1, n]]  右移m， 下移n
m = np.float32([[1, 0, 100], [0, 1, 50]])
img5 = cv2.warpAffine(img, m, dsize)

# 转换矩阵 [[w, 0, 0], [0, h, 0]]  宽度变为原来的w，高度变为原来的h
m1 = np.float32([[0.5, 0, 0], [0, 0.5, 0]])
img6 = cv2.warpAffine(img, m1, dsize)

# 旋转矩阵
m2 = cv2.getRotationMatrix2D((width / 2, height / 2), -60, 0.5)
img7 = cv2.warpAffine(img, m2, dsize)

# 仿射变换
src = np.float32([[0, 0],
                  [width - 10, 0],
                  [0, height - 1]])
dst = np.float32([[50, 50],
                  [width - 100, 80],
                  [100, height - 100]])
m3 = cv2.getAffineTransform(src, dst)
img8 = cv2.warpAffine(img, m3, dsize)

# 透视
src = np.float32([[0, 0],
                  [width - 10, 0],
                  [0, height - 10],
                  [width - 1, height - 1]])
dst = np.float32([[50, 50],
                  [width - 50, 80],
                  [50, height - 100],
                  [width - 100, height - 10]])
m4 = cv2.getPerspectiveTransform(src, dst)
img9 = cv2.warpPerspective(img, m4, dsize)

pic = [("img", img), ("img1", img1), ("img2", img2),
       ("img3", img3), ("img4", img4), ("img5", img5),
       ("img6", img6), ("img7", img7), ("img8", img8),
       ("img9", img9)]
for name, val in pic:
    cv2.imshow(name, val)
    key = cv2.waitKey(5000)
    if key == 27:
        break
cv2.destroyAllWindows()
